using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using Microsoft.DirectX;
using OpenPanekitEditor;
using OpenPanekitEditor.Panel;
using NovodexWrapper;

namespace OriginalPanels
{
    public class Joint: PanelBase
    {
        public Image IconImage { get { return Properties.Resources.ElementIcon_JointX; } }

        Matrix l_to_r_matrix = Matrix.Identity;
        public Matrix LocalToRootMatrix
        {
            get { return l_to_r_matrix; }
            set { l_to_r_matrix = value; }
        }
        Matrix p_to_r_matrix = Matrix.Identity;
        public Matrix PivotToRootMatrix
        {
            get { return p_to_r_matrix; }
            set { p_to_r_matrix = value; }
        }

        public Matrix PivotMatrix
        {
            get { return Matrix.Identity; }
            set { }
        }

        public Joint()
        {
            ParameterDictionary.Add(typeof(JointParameter_SpringCenteor),new JointParameter_SpringCenteor());
            ParameterDictionary.Add(typeof(JointParameter_Brake),new JointParameter_Brake());
        }
#if true
        protected NxRevoluteJoint nx_joint;
        NxSpringDesc nx_jointspring = null;
        protected Vector3 local_axis;
        public override NxActor InitNx(NxScene scene, NxActor parent, Matrix pmatrix)
        {
            Matrix globalpose = pmatrix * parent.getGlobalOrientation();
            current_actor =  NovodexUtil.createBoxActor(scene,Vector3.Empty,globalpose,PanelUtil.PanelDensity);
            current_actor.setGlobalOrientation(globalpose);
            Vector3 world_anchor = Vector3.TransformCoordinate(Vector3.Empty, globalpose);
            Vector3 world_axis = Vector3.TransformNormal(local_axis, globalpose);
            nx_joint = NovodexUtil.createRevoluteJoint(scene,parent,current_actor,world_anchor,world_axis,true);
            
            nx_joint.setLimits(new NxJointLimitPairDesc(-90*NovodexUtil.DEG_TO_RAD, 1, 0, 90*NovodexUtil.DEG_TO_RAD, 1, 0));//lV lR lH - hV hR h
            nx_jointspring = new NxSpringDesc(100f, 10f, 0f);
            //CreateJointShape(parent,current_actor, pmatrix);
            return current_actor;
        }
#else
        protected NxFixedJoint nx_joint;
        NxSpringDesc nx_jointspring = null;
        protected Vector3 local_axis;
        public override NxActor InitNx(NxScene scene, NxActor parent, Matrix pmatrix)
        {
            Matrix globalpose = pmatrix * parent.getGlobalOrientation();
            current_actor = NovodexUtil.createBoxActor(scene, Vector3.Empty, globalpose, PanelUtil.PanelDensity);
            current_actor.setGlobalOrientation(globalpose);
            Vector3 world_anchor = Vector3.TransformCoordinate(Vector3.Empty, globalpose);
            Vector3 world_axis = Vector3.TransformNormal(local_axis, globalpose);
            //nx_joint = NovodexUtil.createCylindricalJoint(scene, parent, current_actor, world_anchor, world_axis, true);
            //nx_joint = NovodexUtil.createFixedJoint(scene, parent, current_actor, world_anchor);
            
            //nx_joint.setLimits(new NxJointLimitPairDesc(-2, 10000, 10000, 2, 10000, 10000));//lV lR lH - hV hR hH            
            nx_jointspring = new NxSpringDesc(500f, 0.1f, 0f);
            CreateJointShape(parent, current_actor, pmatrix);
            return current_actor;
        }
#endif
        public override void UpdateNxParameter(float dt)
        {
            nx_jointspring.targetValue = 0f;
            float ang = (ParameterDictionary[typeof(JointParameter_SpringCenteor)].Value / 180) * (float)Math.PI;
            nx_jointspring.targetValue = ang;
            nx_joint.setSpring(nx_jointspring);
        }
    }
    public class JointParameter_SpringCenteor : OpenPanekitEditor.Panel.Parameter
    {
        public override float UpperLimit
        {
            get { return 90f; }
        }
        public override float LowerLomit
        {
            get { return -90f; }
        }
        public override float Quantize
        {
            get { return 15f; }
        }
        public override String Title
        {
            get { return Resouces.Strings.SettingParameter_JointAngle; }
        }
        public override String Unit
        {
            get { return "DEG"; }
        }
        public override bool CanAssignControl
        {
            get { return true; }
        }
    }
    public class JointParameter_Brake : OpenPanekitEditor.Panel.Parameter
    {
        public override float UpperLimit
        {
            get { return 1000f; }
        }
        public override float LowerLomit
        {
            get { return 0f; }
        }
        public override float Quantize
        {
            get { return 10f; }
        }
        public override String Title
        {
            get { return Resouces.Strings.SettingParameter_JointBrake; }
        }
        public override String Unit
        {
            get { return "N"; }
        }
        public override bool CanAssignControl
        {
            get { return true; }
        }
    }
    [PanelAttribute("Original::JointX",HasPivot=true,HasActor=true,LocalD3DModelName = "Model\\Joint_X_P.X", PivotD3DModelName = "Model\\Joint_X_S.X")]
    public class Joint_X : Joint, IPanel
    {
        public Joint_X():base()
        {
            local_axis = new Vector3(1, 0, 0);
        }
    }
    [PanelAttribute("Original::JointY",HasPivot=true,HasActor=true,LocalD3DModelName = "Model\\Joint_Y_P.X", PivotD3DModelName = "Model\\Joint_Y_S.X")]
    public class Joint_Y : Joint, IPanel
    {
        public Joint_Y()
            : base()
        {
            local_axis = new Vector3(0, 1, 0);
        }
    }
    [PanelAttribute("Original::JointZ",HasPivot=true,HasActor=true,LocalD3DModelName = "Model\\Joint_Z_P.X", PivotD3DModelName = "Model\\Joint_Z_S.X")]
    public class Joint_Z : Joint, IPanel
    {
        public Joint_Z()
            : base()
        {
            local_axis = new Vector3(0, 0, 1);
        }
    }
}
